package com.bootdo.system.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.bootdo.system.domain.SowingmapDO;
import com.bootdo.system.domain.WechatPicDO;
import com.bootdo.system.service.SowingmapService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * @ClassName: SowingmapController
 * @Description: TODO(移动端轮播图主页展示等等)
 * @author lzy
 * @date 2018年6月6日
 *
 */

@Controller
@RequestMapping("/system/sowingmap")
public class SowingmapController {
	@Autowired
	private SowingmapService sowingmapService;
	@Autowired
	private BootdoConfig bootdoConfig;

	/**
	 * 用于更新数据库或新增数据时的注解，更新redis服务器中该value的值
	 */
	@CachePut(value = "redis", key = "#key", condition = "#key<10")
	public String cachePut(String key) throws Exception {
		return "cachePut";
	}

	/**
	 * 用于删除数据操作时的注解，删除redis数据库中该value对应的数据
	 */
	@GetMapping("/clear")
	@CacheEvict(value = "app",allEntries=true)	
	public R cacheEvict(String key) throws Exception {
		
		return R.ok("清除成功");
	}

	@GetMapping()
	String Sowingmap() {
		return "system/sowingmap/sowingmap";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addjoinpolicy
	 * @Description: TODO(移动端加盟政策展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addjoinpolicylist")
	public PageUtils addjoinpolicy(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 3);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addtrain
	 * @Description: TODO(移动端培训展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addtrainlist")
	public PageUtils addtrain(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 4);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addtrain
	 * @Description: TODO(移动端招聘展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addemploylist")
	public PageUtils addemploylist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 5);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addintroducelist
	 * @Description: TODO(移动端介绍展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addintroducelist")
	public PageUtils addintroducelist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 6);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addpaylist
	 * @Description: TODO(移动端移动支付展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addpaylist")
	public PageUtils addpaylist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 7);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: addchatlist
	 * @Description: TODO(移动端社交广告展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addchatlist")
	public PageUtils addchatlist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 8);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}


	

	/**
	 * @Title:
	 * @Description: TODO(移动端业务合作展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/addworklist")
	public PageUtils addworklist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 9);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	// 跳转轮播图
	@GetMapping("/add")
	String add() {
		return "system/sowingmap/add";
	}

	/**
	 * @Title: addpage
	 * @Description: TODO(移动端主页展示添加)
	 * @author: lzy
	 * @date: 2018年6月1日 下午1:57:26
	 */
	@GetMapping("/addpage")
	String addpage() {
		return "system/sowingmap/addpage";
	}

	// 跳转加盟政策
	@GetMapping("/addjoinpolicy")
	String addjoinpolicy() {
		return "system/sowingmap/addjoinpolicy";
	}

	// 跳转公司培训
	@GetMapping("/addtrain")
	String addtrain() {
		return "system/sowingmap/addtrain";
	}

	// 跳转公司招聘
	@GetMapping("/addemploy")
	String addemploy() {
		return "system/sowingmap/addemploy";
	}

	// 跳转公司介绍
	@GetMapping("/addintroduce")
	String addintroduce() {
		return "system/sowingmap/addintroduce";
	}

	// 跳转移动支付
	@GetMapping("/addpay")
	String addpay() {
		return "system/sowingmap/addpay";
	}

	// 跳转社交广告
	@GetMapping("/addchat")
	String addchat() {
		return "system/sowingmap/addchat";
	}

	// 跳转业务合作
	@GetMapping("/addwork")
	String addwork() {
		return "system/sowingmap/addwork";
	}

	// 跳转主页广告
	@GetMapping("/addAdvertisingPicture1")
	String addAdvertisingPicture1() {
		return "system/sowingmap/addAdvertisingPicture";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id, Model model) {
		SowingmapDO sowingmap = sowingmapService.get(id);
		model.addAttribute("sowingmap", sowingmap);
		return "system/sowingmap/edit";
	}
	
	@GetMapping("/sort/{id}")
	String sort(@PathVariable("id") Integer id,Model model) {
		model.addAttribute("id", id);
		return "system/sowingmap/sort";
	}

	
	@ResponseBody
	@PostMapping("/sortUpdate")
	@CacheEvict(value = "app", key = "'homepagelist'")
	public R sortUpdate(SowingmapDO sowingmapDO) {
		int update = sowingmapService.update(sowingmapDO);
		if(update == 1){
			return R.ok();
		}else {
			return R.error();
		}
		
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@CacheEvict(value = "app",allEntries=true)	
	public R update(@RequestParam("file") MultipartFile file, SowingmapDO sowingmap) {
		String URL = null;
		try {
			URL = OssUtil.fileUpload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String imgUrl = sowingmapService.get(sowingmap.getId()).getImgUrl().substring(45, 62);
		OssUtil.deleteBucketInfo(imgUrl);
        sowingmap.setImgUrl(URL); // 服务端
	    int update = sowingmapService.update(sowingmap);
	    if(update == 1){
	    	return R.ok("修改成功");
	    }else {
	    	return R.error("修改失败");
		}
		
	}

    /**
     * 
     * @Title: remove  
     * @Description: TODO(删除移动端图片)
     * @author: lzy
     * @date: 2018年6月12日 下午2:57:55
     */
	@PostMapping("/remove")
	@ResponseBody
	@CacheEvict(value = "app",allEntries=true)	
	public R remove(Integer id) {
		String imgUrl = sowingmapService.get(id).getImgUrl().substring(45, 62);
		if (sowingmapService.remove(id) > 0) {
			OssUtil.deleteBucketInfo(imgUrl);
			return R.ok();
		} else {
			return R.error();
		}
	}



	/**
	 * @Title: upload
	 * @Description: TODO(移动端主页展示图片上传)
	 * @author: lzy
	 * @date: 2018年5月26日 上午9:53:02
	 */
	@ResponseBody
	@PostMapping("/uploads")
	@CacheEvict(value = "app",allEntries=true)	
	R upload(@RequestParam("img") MultipartFile[] img,Integer id) {
		for (MultipartFile file : img) {
			String URL = null;
			try {
				URL = OssUtil.fileUpload(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			SowingmapDO sowingmapDO = new SowingmapDO();
			sowingmapDO.setImgUrl(URL);
			sowingmapDO.setAddTime(new Date());
			sowingmapDO.setType(id);// 1,轮播图2,主页展示3,加盟政策4,培训5，公司招聘,6公司介绍
			sowingmapService.save(sowingmapDO);
		}
         
		return R.ok("上传成功");

	}

	// TODO 移动端主页展示缓存处理

	/**
	 * @Title: lunbolist
	 * @Description: TODO(移动端主页轮播图展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/lunbolist")
	@Cacheable(value = "app", key = "'lunbolist'")
	public PageUtils lunbolist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 1);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: save
	 * @Description: TODO(移动端主页轮播图新增)
	 * @author: lzy
	 * @date: 2018年6月12日 上午9:24:29
	 */
	@ResponseBody
	@PostMapping("/save")
	@CacheEvict(value = "app", key = "'lunbolist'")
	public R save(@RequestParam("img") MultipartFile[] img) {
		try {
			for (MultipartFile file : img) {
				String URL = null;
				try {
					URL = OssUtil.fileUpload(file);
				} catch (IOException e) {
					e.printStackTrace();
				}
				SowingmapDO sowingmapDO = new SowingmapDO();
				sowingmapDO.setImgUrl(URL);
				sowingmapDO.setAddTime(new Date());
				sowingmapDO.setType(1);// 1代表轮播图
				sowingmapDO.setSort(sowingmapDO.getSort());
				sowingmapService.save(sowingmapDO);
			}
		} catch (Exception e) {
			return R.error("上传失败");
		 }
		return R.ok("上传成功");
	}
	

	

	/**
	 * @Title: save
	 * @Description: TODO(移动端主页展示图新增)
	 * @author: lzy
	 * @date: 2018年6月12日 上午9:24:29
	 */
	@ResponseBody
	@PostMapping("/savePage")
	@CacheEvict(value = "app", key = "'homepagelist'")
	public R savePage(@RequestParam("img") MultipartFile[] img, SowingmapDO sowingmapDO) {
		String URL = null;
		for (MultipartFile file : img) {
			try {
				URL = OssUtil.fileUpload(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			sowingmapDO.setImgUrl(URL);
			sowingmapDO.setAddTime(new Date());
			sowingmapDO.setType(2);// 2代表主页展示
			sowingmapDO.setSort(sowingmapDO.getSort());
			sowingmapService.save(sowingmapDO); 			
		}
		return R.ok();

	}
	
	
	
	/**
	 * 
	 * @Title: addAdvertisingPicture  
	 * @Description: TODO(移动端广告轮播图新增)
	 * @author: lzy
	 * @date: 2018年6月12日 下午3:14:22
	 */
	@ResponseBody
	@PostMapping("/addAdvertisingPicture")
	@CacheEvict(value = "app", key = "'addAdvertisingPicturelist'")
	public R addAdvertisingPicture(@RequestParam("img") MultipartFile[] img, SowingmapDO sowingmapDO) {
		String URL = null;
		for (MultipartFile file : img) {
			try {
				URL = OssUtil.fileUpload(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			sowingmapDO.setImgUrl(URL);
			sowingmapDO.setAddTime(new Date());
			sowingmapDO.setType(10);// 10代表广告轮播图
			sowingmapDO.setSort(sowingmapDO.getSort());
			sowingmapService.save(sowingmapDO);
		}
		return R.ok();
	}
	
	

	/**
	 * @Title: addworklist
	 * @Description: TODO(移动端主页广告轮播图展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@Cacheable(value = "app", key = "'addAdvertisingPicturelist'")
	@GetMapping("/addAdvertisingPicturelist")
	public PageUtils addAdvertisingPicturelist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 10);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

	/**
	 * @Title: homepagelist
	 * @Description: TODO(移动端主页图片展示)
	 * @author: lzy
	 * @date: 2018年6月1日 下午2:52:46
	 */
	@ResponseBody
	@GetMapping("/homepagelist")
	@Cacheable(value = "app", key = "'homepagelist'")
	public PageUtils homepagelist(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("type", 2);
		Query query = new Query(params);
		List<SowingmapDO> sowingmapList = sowingmapService.list(query);
		int total = sowingmapService.count(query);
		PageUtils pageUtils = new PageUtils(sowingmapList, total);
		return pageUtils;
	}

}
